package cn.edu.gzhu.workOrderManagement.typeHandles;

import java.sql.*;
import java.util.HashMap;

import cn.hutool.json.JSONUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;


/**
 * @author chen xi
 * 用来转换WorkOrder中的HashMap
 */


public class WorkOrderMapTypeHandler extends BaseTypeHandler<HashMap<String, Object>> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, HashMap<String, Object> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSONUtil.toJsonStr(parameter));

    }

    @Override
    public HashMap<String, Object> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String sqlJson = rs.getString(columnName);
        return sqlJson == null ? null : JSONUtil.toBean(sqlJson, HashMap.class);
    }

    @Override
    public HashMap<String, Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String sqlJson = rs.getString(columnIndex);
        return sqlJson == null ? null : JSONUtil.toBean(sqlJson, HashMap.class);
    }

    @Override
    public HashMap<String, Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String sqlJson = cs.getString(columnIndex);
        return sqlJson == null ? null : JSONUtil.toBean(sqlJson, HashMap.class);
    }
}
